#include "instancecall.h"
#include <thread>


InstanceCall::InstanceCall():
    bRecv{false}
{
    run();
    call_func();
}

void InstanceCall::run()
{
    std::thread([this]()mutable{
        try
        {
            if(!_mb_ptr)
                throw ERR_DEVICE_MODBUS_DISCONNECTED;
            std::this_thread::sleep_for(std::chrono::seconds(5));
            int sig = 0;
            bRecv = true;
            while(sig_que.size())
                sig_que.pop();
            cout<<"send cad list:"<<endl;
            // cs.send_cad_list();
            cout<<"start recv message : "<<endl;
            while(bRecv)
            {
                std::this_thread::sleep_for(std::chrono::milliseconds(50));
                _mb_ptr->recv_msg();
                sig = _mb_ptr->deal_msg();
                if(sig == -1)
                    continue;
                if(sig > 0 && sig < 99)
                    sig_que.push(sig);
                _mb_ptr->send_msg();
            }
            cout<<"quit loop to send and recv msg"<<endl;
        }catch(ReturnStatus error_code){
            cout<<getErrString(error_code)<<endl;
        }catch(...){
            cout<<getErrString(ERR_FUNC_SERVICE_PROCESS_RUN_ERROR)<<endl;
        }
        bRecv = false;
        while(sig_que.size())
            sig_que.pop();
        cout<<"quit loop to stop master"<<endl;
    }).detach();
}

void InstanceCall::call_func()
{
    std::thread([this]()mutable{
        cout<<"start call func :"<<endl;
        while(bRecv)
        {
            while(sig_que.size())
            {
                int sig = sig_que.front();
                func(sig);
                sig_que.pop();
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        }
        cout<<"quit loop to call func !"<<endl;
    }).detach();
}


void InstanceCall::func(int sig)
{
    std::thread([this,sig]()mutable{
        try
        {
            cout<<"[SIGNAL] : "<<sig<<endl;
            _mb_ptr->ReceiveReply(sig);
            int seamId = 0;
            if(sig > 0 && sig < 5)
                seamId = _mb_ptr->get_seamId();
            switch(sig)
            {
            case 1:
            case 2:// 0 空走 1 焊接 2 空走后焊接
                // cs.setActionType(0);
                // cs.autoScanWelding(seamId);
                break;
            case 3:
                // cs.setActionType(1);
                // cs.autoScanWelding(seamId);
                break;
            case 4:
                // cs.setActionType(1);
                // cs.autoScanWelding(seamId);
                break;
            case 5://同步焊缝信息
                // cs.syncSeamInfo();
                break;
            case 6:
                // cs.cleanTorch();
                break;
            case 7://传感器位置校验
                // cs.checkSensor();
                break;
            case 10:
                // cs.NewSyncMotorAngle();
                break;
            case 11:
                // cs.back_home();
                break;
            case 15:
            {
                // int cadId = _mb_ptr->get_cadId();
                // cs.transferDBData(cadId);
            }
                break;
            case 16:
                // cs.unloading_cad();
                break;
            case 17:
                // cs.loading_cad();
                break;
            default:
                break;
            }
            cout<<"[func][OVER]"<<endl;
        }catch(ReturnStatus error_code){
            cout<<getErrString(error_code)<<endl;
        }catch(...){
            cout<<"func execute failed ."<<endl;
        }
    }).detach();
}

